package com.miyake.loveviewweb.client;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.Window.Navigator;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.account.CreateAccount;
import com.miyake.loveviewweb.client.account.CreateAccountListener;
import com.miyake.loveviewweb.client.account.LoginInfo;
import com.miyake.loveviewweb.client.chart.FlashChart;
import com.miyake.loveviewweb.client.main.MainUI;
import com.miyake.loveviewweb.client.plugin.PluginManager;
import com.miyake.loveviewweb.client.plugin.PluginRegister;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.shared.model.ChartModel;

public class Main implements EntryPoint {
//    private MainUI mainUi = null;
    private CreateAccount createAccount = null;
    private RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get();

    public void onModuleLoad() { 
    
        if (isDefaultPage()) {           
            // Try to login anyways
            DataService.Util.getInstance().login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {

                public void onFailure(Throwable caught) {
  
                }

                public void onSuccess(LoginInfo result) {
                    // Already logged in this application
                    if (result.isLoggedInLoveView()) {
                        rootLayoutPanel.clear();
                        rootLayoutPanel.add(getMainUi());
                    }
                    // Logged in google but has not yet had an account for this application
                    else if (result.isLoggedInGoogle()) {
                        //Window.Location.assign(result.getLoginUrl() + "?Startup=CreateAccount");
                        setContentPanel(getCreateAccount(result.getEmail(), result.getNickname()));
                    }
                    else {
                        Window.Location.assign(result.getLoginUrl());
                    }
                }
                
            });
        }
        else if (isPluginMarketPage()) {
            setContentPanel(new PluginManager(Location.getParameter("filter")));
        }
        else if (isPluginProviderPage()) {
            setContentPanel(new PluginRegister());
        }
        else if (isDynamicChartTestPage()) {
            final FlashChart chart = new FlashChart();
            setContentPanel(chart.getUiWidget());

            String skey = Location.getParameter("key");
            DataServiceWrapper.getInstance().getService().stringToKey(skey, new AsyncCallback<Key>() {

                public void onFailure(Throwable caught) {
                    // TODO Auto-generated method stub
                            
                }

                public void onSuccess(Key result) {

                    DataServiceWrapper.getInstance().getService().getChart(result, true, ChartModel.ChartEngine.DEFAULT_FLASH_CHART, new AsyncCallback<ChartModel>() {

                        public void onFailure(Throwable caught) {
                            Window.alert(caught.getMessage());
                        }


                        public void onSuccess(ChartModel result) {
                            chart.setData(result);
                        }
                    });
                }
            });           
        }
    }

    private boolean isDynamicChartTestPage() {
        return Location.getParameter("startup").equals("dynamicChartTest");
    }

    private boolean isPluginProviderPage() {
        return Location.getParameter("startup").equals("pluginProvider");
    }

    private boolean isPluginMarketPage() {
        return Location.getParameter("startup").equals("pluginMarket");
    }

    private boolean isDefaultPage() {
        return Location.getParameter("startup") == null;
    }
    
    private void setContentPanel(Widget widget) {
        rootLayoutPanel.clear();
        rootLayoutPanel.add(widget);
    }
            
    protected Widget getMainUi() {
        if (Navigator.getUserAgent().contains("Android")) {
          //  this.mainUi = new MobileMainUI();
            return new MainUI();
        }
        else {
            return new MainUI();
        }
    }

    private Widget getCreateAccount(String email, String nickname) {
        if (createAccount == null) {
            createAccount = new CreateAccount(new CreateAccountListener() {
                public void onCreateUser() {
                    setContentPanel(getMainUi());
                }
            }, email, nickname);
        }
        return createAccount;
    }
}